﻿Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core

Public Class PowerPointUtilities
    Dim objPPT As PowerPoint.Application
    Dim objPres As PowerPoint.Presentation
    Dim currentSlideNo = 1

    Public Function StartPowerPoint() As Boolean
        objPPT = New PowerPoint.Application
        objPPT.Visible = MsoTriState.msoTrue
        objPPT.WindowState = PowerPoint.PpWindowState.ppWindowMaximized

        Return True
    End Function

    Public Function OpenFile(ByVal filePath As String) As Boolean
        EnsurePowerPointIsRunning(False, False)
        objPres = objPPT.Presentations.Open(filePath, MsoTriState.msoCTrue, MsoTriState.msoFalse, MsoTriState.msoCTrue)
        Dim objSSS As Microsoft.Office.Interop.PowerPoint.SlideShowSettings
        'Run the Slide show
        objSSS = objPres.SlideShowSettings
        objSSS.Run()
        Return True
    End Function

    Public Function PageDown() As Boolean
        'Dim objSlide As PowerPoint.Slide
        'objSlide = objPres.Slides(Math.Min(objPres.Slides.Count, currentSlideNo + 1))
        'objSlide.Select()
        'currentSlideNo = Math.Min(objPres.Slides.Count, currentSlideNo + 1)

        Dim oSlideShowView = objPres.SlideShowWindow.View
        oSlideShowView.Next()

        Return True
    End Function

    Public Function PageUp() As Boolean
        'Dim objSlide As PowerPoint.Slide
        'objSlide = objPres.Slides(Math.Max(1, currentSlideNo - 1))
        'objSlide.Select()
        'currentSlideNo = Math.Max(1, currentSlideNo - 1)

        Dim oSlideShowView = objPres.SlideShowWindow.View
        oSlideShowView.Previous()

        Return True
    End Function

    Public Function Quit() As Boolean
        Try
            objPres.Close()
            objPres = Nothing
        Catch
        End Try
        Try
            objPPT.Quit()
            objPPT = Nothing
        Catch ex As Exception
        End Try
        System.GC.Collect()

        Return True
    End Function

    Sub EnsurePowerPointIsRunning(Optional ByVal blnAddPresentation As Boolean = False, Optional ByVal blnAddSlide As Boolean = False)
        Dim strName As String
        '
        'Try accessing the name property. If it causes an exception then 
        'start a new instance of PowerPoint
        Try
            strName = objPPT.Name
        Catch ex As Exception
            StartPowerPoint()
        End Try
        '
        'blnAddPresentation is used to ensure there is a presentation loaded
        If blnAddPresentation = True Then
            Try
                strName = objPres.Name
            Catch ex As Exception
                objPres = objPPT.Presentations.Add(MsoTriState.msoTrue)
            End Try
        End If
        '
        'BlnAddSlide is used to ensure there is at least one slide in the 
        'presentation
        If blnAddSlide Then
            Try
                strName = objPres.Slides(1).Name
            Catch ex As Exception
                Dim objSlide As PowerPoint.Slide
                Dim objCustomLayout As PowerPoint.CustomLayout
                objCustomLayout = objPres.SlideMaster.CustomLayouts.Item(1)
                objSlide = objPres.Slides.AddSlide(1, objCustomLayout)
                objSlide.Layout = PowerPoint.PpSlideLayout.ppLayoutText
                objCustomLayout = Nothing
                objSlide = Nothing
            End Try
        End If
    End Sub
End Class
